Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 709)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.00279 13.00046 12.99816 12.99590 12.99367 12.99147 12.98929 12.98714
## [9] 12.98500 12.98289 12.98079 12.97870 12.97663 12.97456 12.97250 12.97045
## [17] 12.96839 12.96634 12.96428 12.96221 12.96013 12.95805 12.95595 12.95383
## [25] 12.95170 12.94954 12.94736 12.94516 12.94293 12.94066 12.93837 12.93603
## [33] 12.93366 12.93125 12.92880 12.92630 12.92375 12.92114 12.91848 12.91577
## [41] 12.91302 12.91023 12.90741 12.90456 12.90168 12.89878 12.89587 12.89295
## [49] 12.89002 12.88708 12.88415 12.88122 12.87831 12.87541 12.87253 12.86967
## [57] 12.86685 12.86405 12.86130 12.85858 12.85592 12.85330 12.85074 12.84824
## [65] 12.84580 12.84343 12.84114 12.83892 12.83679 12.83474 12.83279 12.83093
## [73] 12.82917 12.82751 12.82596 12.82453 12.82321 12.82202 12.82071 12.81906
## [81] 12.81708 12.81479 12.81223 12.80939 12.80632 12.80302 12.79951 12.79583
## [89] 12.79198 12.78799 12.78388 12.77967 12.77537 12.77102 12.76663 12.76221
## [97] 12.75780 12.75341 12.74906 12.74477 12.74057 12.73646 12.73248 12.72865
## [105] 12.72497 12.72148 12.71820 12.71514 12.71232 12.70977 12.70751 12.70555
## [113] 12.70391 12.70263 12.70171 12.70118 12.70106 12.70136 12.70212 12.70334
## [121] 12.70526 12.70805 12.71163 12.71594 12.72092 12.72648 12.73256 12.73910
## [129] 12.74602 12.75326 12.76075 12.76841 12.77618 12.78400 12.79178 12.79947
## [137] 12.80699 12.81428 12.82126 12.82787 12.83405 12.83971 12.84641 12.85557
## [145] 12.86694 12.88028 12.89533 12.91183 12.92955 12.94821 12.96758 12.98741
## [153] 13.00743 13.02740 13.04708 13.06619 13.08451 13.10176 13.11771 13.13210
## [161] 13.14467 13.15519 13.16339 13.17109 13.18019 13.19053 13.20199 13.21441
## [169] 13.22766 13.24159 13.25605 13.27092 13.28605 13.30129 13.31650 13.33155
## [177] 13.34628 13.36056 13.37424 13.38719 13.39926 13.41030 13.42019 13.42877
## [185] 13.43590 13.44144 13.44526 13.44720 13.44712 13.44565 13.44349 13.44066
## [193] 13.43716 13.43299 13.42817 13.42271 13.41660 13.40985 13.40248 13.39449
## [201] 13.38588 13.37667 13.36685 13.35644 13.34544 13.33386 13.32171 13.30899
## [209] 13.29572 13.28188 13.26751 13.25259 13.23542 13.21454 13.19036 13.16326
## [217] 13.13365 13.10192 13.06846 13.03369 12.99799 12.96175 12.92539 12.88929
## [225] 12.85384 12.81946 12.78653 12.75546 12.72663 12.70045 12.67731 12.65454
## [233] 12.62937 12.60208 12.57294 12.54224 12.51024 12.47723 12.44348 12.40926
## [241] 12.37485 12.34053 12.30658 12.27326 12.24086 12.20965 12.17990 12.15190
## [249] 12.12591 12.10222 12.08110 12.06283 12.04716 12.03352 12.02169 12.01145
## [257] 12.00256 11.99481 11.98796 11.98180 11.97610 11.97064 11.96519 11.95953
## [265] 11.95343 11.94667 11.93902 11.93027 11.92200 11.91583 11.91156 11.90896
## [273] 11.90784 11.90797 11.90914 11.91115 11.91379 11.91684 11.92010 11.92334
## [281] 11.92637 11.92897 11.93093 11.93203 11.93208 11.93085 11.92814 11.92373
## [289] 11.91742 11.91043 11.90410 11.89833 11.89304 11.88814 11.88355 11.87919
## [297] 11.87497 11.87079 11.86659 11.86228 11.85776 11.85295 11.84778 11.84214
## [305] 11.83597 11.82917 11.82166 11.81335 11.80327 11.79065 11.77579 11.75895
## [313] 11.74040 11.72043 11.69929 11.67727 11.65464 11.63167 11.60864 11.58581
## [321] 11.56347 11.54188 11.52132 11.50207 11.48438 11.46855 11.45484 11.44352
## [329] 11.43488 11.42612 11.41462 11.40085 11.38527 11.36838 11.35063 11.33250
## [337] 11.31448 11.29702 11.28061 11.26572 11.25282 11.24239 11.23490 11.23083
## [345] 11.23065 11.23339 11.23769 11.24347 11.25065 11.25914 11.26886 11.27971
## [353] 11.29161 11.30448 11.31823 11.33278 11.34803 11.36391 11.38032 11.39719
## [361] 11.41442 11.43193 11.44963 11.46744 11.48528 11.50305 11.52271 11.54603
## [369] 11.57267 11.60226 11.63444 11.66886 11.70516 11.74299 11.78198 11.82179
## [377] 11.86204 11.90240 11.94249 11.98197 12.02048 12.05765 12.09314 12.12658
## [385] 12.15762 12.18590 12.21106 12.23605 12.26379 12.29397 12.32624 12.36025
## [393] 12.39569 12.43221 12.46946 12.50712 12.54485 12.58231 12.61916 12.65507
## [401] 12.68970 12.72271 12.75377 12.78253 12.80866 12.83183 12.85169 12.86792
## [409] 12.88171 12.89454 12.90641 12.91737 12.92742 12.93660 12.94493 12.95244
## [417] 12.95915 12.96508 12.97025 12.97470 12.97845 12.98152 12.98394 12.98572
## [425] 12.98690 12.98750 12.98755 12.98572 12.98087 12.97327 12.96318 12.95090
## [433] 12.93667 12.92078 12.90350 12.88510 12.86585 12.84603 12.82590 12.80574
## [441] 12.78582 12.76641 12.74778 12.73021 12.71397 12.69932 12.68655 12.67592
## [449] 12.66473 12.65041 12.63340 12.61414 12.59308 12.57066 12.54733 12.52354
## [457] 12.49972 12.47633 12.45381 12.43260 12.41314 12.39590 12.38129 12.36979
## [465] 12.35960 12.34872 12.33727 12.32534 12.31304 12.30047 12.28773 12.27494
## [473] 12.26219 12.24958 12.23723 12.22523 12.21369 12.20271 12.19240 12.18286
## [481] 12.17419 12.16651 12.15990 12.15448 12.15035 12.14728 12.14493 12.14328
## [489] 12.14230 12.14196 12.14222 12.14305 12.14442 12.14631 12.14867 12.15148
## [497] 12.15471 12.15832 12.16228 12.16657 12.17115 12.17599 12.18105 12.18632
## [505] 12.19292 12.20190 12.21302 12.22608 12.24085 12.25712 12.27468 12.29330
## [513] 12.31277 12.33287 12.35338 12.37410 12.39480 12.41526 12.43526 12.45460
## [521] 12.47306 12.49041 12.50644 12.52093 12.53368 12.54445 12.55303 12.56132
## [529] 12.57124 12.58261 12.59526 12.60903 12.62373 12.63920 12.65526 12.67175
## [537] 12.68848 12.70529 12.72200 12.73845 12.75446 12.76986 12.78447 12.79813
## [545] 12.81066 12.82189 12.83164 12.83975 12.84605 12.85035 12.85249 12.85230
## [553] 12.84960 12.84474 12.83824 12.83024 12.82084 12.81016 12.79832 12.78545
## [561] 12.77166 12.75707 12.74180 12.72596 12.70968 12.69307 12.67625 12.65935
## [569] 12.64247 12.62575 12.60929 12.59322 12.57765 12.56271 12.54534 12.52289
## [577] 12.49606 12.46555 12.43207 12.39632 12.35899 12.32080 12.28243 12.24461
## [585] 12.20802 12.17336 12.14135 12.11269 12.08806 12.06819 12.04935 12.02759
## [593] 12.00328 11.97679 11.94847 11.91869 11.88782 11.85620 11.82422 11.79222
## [601] 11.76057 11.72964 11.69979 11.67138 11.64477 11.62033 11.59842 11.57940
## [609] 11.56363 11.55148 11.54204 11.53411 11.52760 11.52242 11.51848 11.51571
## [617] 11.51402 11.51331 11.51351 11.51452 11.51626 11.51865 11.52160 11.52501
## [625] 11.52882 11.53292 11.53724 11.54168 11.54617 11.55061 11.55588 11.56280
## [633] 11.57125 11.58107 11.59214 11.60431 11.61744 11.63139 11.64603 11.66121
## [641] 11.67680 11.69265 11.70862 11.72459 11.74040 11.75591 11.77100 11.78552
## [649] 11.79932 11.81227 11.82424 11.83610 11.84879 11.86221 11.87630 11.89096
## [657] 11.90612 11.92168 11.93758 11.95373 11.97005 11.98645 12.00285 12.01917
## [665] 12.03533 12.05125 12.06685 12.08241 12.09829 12.11447 12.13094 12.14770
## [673] 12.16473 12.18203 12.19957 12.21737 12.23539 12.25363 12.27209 12.29075
## [681] 12.30960 12.32864 12.34785 12.36722 12.38674 12.40640 12.42620 12.44611
## [689] 12.46614 12.48627 12.50649 12.52679 12.54716 12.56755 12.58793 12.60830
## [697] 12.62870 12.64913 12.66962 12.69019 12.71085 12.73163 12.75254 12.77360
## [705] 12.79484 12.81626 12.83789 12.85974 12.88185
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 709)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62191 12.61752 12.61323 12.60903 12.60493 12.60092 12.59700 12.59317
## [9] 12.58942 12.58576 12.58218 12.57868 12.57526 12.57192 12.56865 12.56546
## [17] 12.56233 12.55928 12.55629 12.55337 12.55052 12.54772 12.54499 12.54232
## [25] 12.53970 12.53713 12.53462 12.53216 12.52975 12.52739 12.52507 12.52280
## [33] 12.52057 12.51838 12.51622 12.51410 12.51201 12.50993 12.50786 12.50582
## [41] 12.50380 12.50181 12.49985 12.49792 12.49604 12.49421 12.49242 12.49068
## [49] 12.48900 12.48738 12.48582 12.48433 12.48291 12.48157 12.48031 12.47914
## [57] 12.47805 12.47705 12.47615 12.47534 12.47464 12.47405 12.47357 12.47321
## [65] 12.47296 12.47284 12.47284 12.47298 12.47325 12.47366 12.47421 12.47491
## [73] 12.47576 12.47677 12.47793 12.47926 12.48075 12.48241 12.48421 12.48610
## [81] 12.48808 12.49015 12.49231 12.49457 12.49692 12.49935 12.50188 12.50450
## [89] 12.50721 12.51001 12.51289 12.51587 12.51894 12.52209 12.52533 12.52867
## [97] 12.53209 12.53559 12.53919 12.54287 12.54664 12.55049 12.55443 12.55846
## [105] 12.56257 12.56677 12.57106 12.57543 12.57988 12.58442 12.58904 12.59375
## [113] 12.59854 12.60341 12.60837 12.61341 12.61854 12.62374 12.62903 12.63440
## [121] 12.63994 12.64573 12.65174 12.65796 12.66437 12.67093 12.67763 12.68444
## [129] 12.69135 12.69833 12.70537 12.71243 12.71950 12.72655 12.73357 12.74053
## [137] 12.74741 12.75418 12.76083 12.76734 12.77516 12.78559 12.79838 12.81325
## [145] 12.82995 12.84821 12.86776 12.88835 12.90970 12.93156 12.95366 12.97573
## [153] 12.99751 13.01875 13.03916 13.05850 13.07649 13.09288 13.10739 13.11977
## [161] 13.12975 13.13967 13.15185 13.16602 13.18191 13.19923 13.21771 13.23708
## [169] 13.25705 13.27735 13.29770 13.31782 13.33745 13.35629 13.37408 13.39054
## [177] 13.40539 13.41835 13.42915 13.43751 13.44315 13.44580 13.44621 13.44532
## [185] 13.44321 13.43991 13.43548 13.42996 13.42341 13.41587 13.40740 13.39805
## [193] 13.38786 13.37688 13.36518 13.35278 13.33976 13.32614 13.31200 13.29737
## [201] 13.28230 13.26685 13.25106 13.23499 13.21868 13.20219 13.18556 13.16885
## [209] 13.15210 13.13537 13.11622 13.09259 13.06510 13.03436 13.00099 12.96560
## [217] 12.92881 12.89123 12.85347 12.81615 12.77988 12.74528 12.71296 12.68354
## [225] 12.65763 12.63585 12.61516 12.59228 12.56748 12.54100 12.51312 12.48408
## [233] 12.45414 12.42356 12.39260 12.36151 12.33054 12.29997 12.27004 12.24101
## [241] 12.21314 12.18669 12.16190 12.13905 12.11838 12.10016 12.08464 12.07146
## [249] 12.06001 12.05013 12.04169 12.03454 12.02853 12.02352 12.01936 12.01591
## [257] 12.01303 12.01056 12.00837 12.00630 12.00422 12.00197 11.99942 11.99641
## [265] 11.99281 11.98847 11.98540 11.98552 11.98849 11.99398 12.00166 12.01120
## [273] 12.02227 12.03454 12.04769 12.06137 12.07526 12.08904 12.10236 12.11490
## [281] 12.12634 12.13633 12.14455 12.15067 12.15436 12.15529 12.15313 12.14963
## [289] 12.14670 12.14422 12.14208 12.14016 12.13834 12.13651 12.13456 12.13235
## [297] 12.12979 12.12675 12.12312 12.11878 12.11362 12.10751 12.10035 12.09125
## [305] 12.07955 12.06551 12.04940 12.03147 12.01197 11.99117 11.96932 11.94668
## [313] 11.92351 11.90006 11.87659 11.85336 11.83063 11.80865 11.78768 11.76797
## [321] 11.74979 11.73340 11.71904 11.70698 11.69455 11.67915 11.66120 11.64110
## [329] 11.61925 11.59605 11.57191 11.54724 11.52242 11.49787 11.47399 11.45119
## [337] 11.42986 11.41040 11.39323 11.37874 11.36734 11.35943 11.35542 11.35403
## [345] 11.35370 11.35441 11.35612 11.35880 11.36242 11.36695 11.37235 11.37859
## [353] 11.38564 11.39346 11.40204 11.41132 11.42129 11.43190 11.44313 11.45494
## [361] 11.46731 11.48020 11.49357 11.50740 11.52342 11.54313 11.56620 11.59226
## [369] 11.62097 11.65197 11.68490 11.71943 11.75518 11.79181 11.82897 11.86630
## [377] 11.90346 11.94008 11.97582 12.01032 12.04323 12.07420 12.10288 12.12890
## [385] 12.15192 12.17451 12.19930 12.22602 12.25441 12.28423 12.31520 12.34706
## [393] 12.37957 12.41245 12.44546 12.47832 12.51079 12.54260 12.57348 12.60319
## [401] 12.63147 12.65804 12.68266 12.70507 12.72499 12.74219 12.75815 12.77447
## [409] 12.79105 12.80778 12.82456 12.84128 12.85783 12.87411 12.89002 12.90545
## [417] 12.92029 12.93444 12.94779 12.96024 12.97168 12.98200 12.99111 12.99888
## [425] 13.00523 13.01004 13.01321 13.01424 13.01282 13.00916 13.00345 12.99586
## [433] 12.98659 12.97584 12.96379 12.95063 12.93655 12.92175 12.90641 12.89073
## [441] 12.87489 12.85908 12.84350 12.82833 12.81377 12.80000 12.78722 12.77561
## [449] 12.76248 12.74536 12.72481 12.70140 12.67569 12.64824 12.61961 12.59036
## [457] 12.56106 12.53226 12.50453 12.47843 12.45451 12.43335 12.41550 12.40153
## [465] 12.38875 12.37427 12.35831 12.34108 12.32282 12.30375 12.28408 12.26404
## [473] 12.24385 12.22373 12.20391 12.18461 12.16605 12.14845 12.13203 12.11702
## [481] 12.10364 12.09212 12.08266 12.07551 12.07087 12.06776 12.06505 12.06277
## [489] 12.06093 12.05954 12.05863 12.05820 12.05828 12.05887 12.06001 12.06170
## [497] 12.06396 12.06680 12.07025 12.07432 12.07903 12.08438 12.09041 12.09712
## [505] 12.10593 12.11802 12.13312 12.15093 12.17117 12.19355 12.21778 12.24357
## [513] 12.27063 12.29868 12.32743 12.35659 12.38586 12.41497 12.44363 12.47154
## [521] 12.49841 12.52397 12.54792 12.56997 12.58983 12.60723 12.62186 12.63633
## [529] 12.65328 12.67246 12.69362 12.71651 12.74088 12.76648 12.79306 12.82037
## [537] 12.84816 12.87618 12.90419 12.93193 12.95916 12.98562 13.01106 13.03524
## [545] 13.05791 13.07881 13.09770 13.11433 13.12844 13.13980 13.14814 13.15322
## [553] 13.15479 13.15346 13.15006 13.14475 13.13766 13.12894 13.11872 13.10714
## [561] 13.09434 13.08046 13.06564 13.05002 13.03374 13.01694 12.99975 12.98232
## [569] 12.96479 12.94729 12.92997 12.91296 12.89641 12.88044 12.86222 12.83922
## [577] 12.81207 12.78141 12.74787 12.71210 12.67472 12.63638 12.59770 12.55934
## [585] 12.52191 12.48606 12.45242 12.42163 12.39432 12.37114 12.34814 12.32126
## [593] 12.29096 12.25772 12.22197 12.18419 12.14482 12.10435 12.06321 12.02187
## [601] 11.98079 11.94043 11.90125 11.86371 11.82826 11.79538 11.76551 11.73911
## [609] 11.71665 11.69858 11.68274 11.66670 11.65057 11.63447 11.61850 11.60278
## [617] 11.58740 11.57248 11.55813 11.54445 11.53155 11.51955 11.50855 11.49865
## [625] 11.48997 11.48262 11.47670 11.47232 11.46960 11.46863 11.46975 11.47311
## [633] 11.47852 11.48581 11.49481 11.50532 11.51719 11.53023 11.54426 11.55911
## [641] 11.57459 11.59054 11.60678 11.62312 11.63939 11.65542 11.67102 11.68603
## [649] 11.70025 11.71352 11.72566 11.73776 11.75095 11.76514 11.78024 11.79617
## [657] 11.81282 11.83012 11.84795 11.86625 11.88491 11.90384 11.92296 11.94216
## [665] 11.96136 11.98047 11.99940 12.01847 12.03809 12.05824 12.07891 12.10008
## [673] 12.12174 12.14389 12.16649 12.18955 12.21306 12.23699 12.26133 12.28608
## [681] 12.31122 12.33674 12.36262 12.38886 12.41543 12.44233 12.46955 12.49707
## [689] 12.52487 12.55296 12.58130 12.60990 12.63874 12.66773 12.69683 12.72605
## [697] 12.75542 12.78497 12.81472 12.84470 12.87493 12.90543 12.93624 12.96738
## [705] 12.99887 13.03075 13.06302 13.09572 13.12888
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 709)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.99382 11.99017 11.98659 11.98307 11.97961 11.97621 11.97286 11.96957
## [9] 11.96632 11.96312 11.95996 11.95684 11.95375 11.95070 11.94769 11.94469
## [17] 11.94173 11.93878 11.93585 11.93294 11.93004 11.92715 11.92427 11.92139
## [25] 11.91851 11.91563 11.91274 11.90984 11.90694 11.90402 11.90108 11.89812
## [33] 11.89514 11.89213 11.88909 11.88602 11.88291 11.87976 11.87658 11.87335
## [41] 11.87007 11.86674 11.86336 11.85989 11.85633 11.85267 11.84892 11.84510
## [49] 11.84121 11.83726 11.83327 11.82924 11.82517 11.82109 11.81699 11.81289
## [57] 11.80879 11.80471 11.80066 11.79664 11.79266 11.78873 11.78486 11.78106
## [65] 11.77734 11.77371 11.77018 11.76675 11.76344 11.76025 11.75719 11.75428
## [73] 11.75151 11.74891 11.74648 11.74422 11.74216 11.74029 11.73863 11.73718
## [81] 11.73595 11.73496 11.73421 11.73371 11.73347 11.73322 11.73267 11.73186
## [89] 11.73081 11.72954 11.72809 11.72647 11.72471 11.72284 11.72089 11.71886
## [97] 11.71681 11.71473 11.71267 11.71065 11.70869 11.70682 11.70506 11.70343
## [105] 11.70197 11.70070 11.69965 11.69883 11.69827 11.69801 11.69805 11.69844
## [113] 11.69919 11.70033 11.70189 11.70388 11.70634 11.70929 11.71276 11.71676
## [121] 11.72133 11.72649 11.73227 11.73869 11.74577 11.75354 11.76323 11.77581
## [129] 11.79089 11.80812 11.82714 11.84756 11.86904 11.89119 11.91366 11.93608
## [137] 11.95808 11.97930 11.99936 12.01791 12.03457 12.04898 12.06394 12.08226
## [145] 12.10361 12.12763 12.15400 12.18236 12.21238 12.24371 12.27601 12.30895
## [153] 12.34218 12.37536 12.40814 12.44019 12.47116 12.50072 12.52851 12.55421
## [161] 12.57746 12.59793 12.61528 12.63170 12.64950 12.66853 12.68863 12.70962
## [169] 12.73135 12.75365 12.77636 12.79931 12.82235 12.84531 12.86803 12.89034
## [177] 12.91208 12.93309 12.95320 12.97226 12.99009 13.00654 13.02144 13.03463
## [185] 13.04594 13.05522 13.06230 13.06702 13.06920 13.06916 13.06736 13.06389
## [193] 13.05884 13.05232 13.04441 13.03521 13.02481 13.01331 13.00081 12.98739
## [201] 12.97316 12.95820 12.94261 12.92649 12.90992 12.89301 12.87585 12.85853
## [209] 12.84114 12.82379 12.80656 12.78956 12.77006 12.74568 12.71696 12.68445
## [217] 12.64871 12.61028 12.56973 12.52760 12.48445 12.44082 12.39728 12.35437
## [225] 12.31264 12.27265 12.23494 12.20008 12.16861 12.14109 12.11807 12.09688
## [233] 12.07460 12.05136 12.02732 12.00262 11.97739 11.95179 11.92596 11.90004
## [241] 11.87419 11.84853 11.82322 11.79841 11.77423 11.75083 11.72836 11.70695
## [249] 11.68676 11.66793 11.65059 11.63491 11.62088 11.60832 11.59707 11.58697
## [257] 11.57787 11.56961 11.56202 11.55496 11.54827 11.54179 11.53535 11.52881
## [265] 11.52201 11.51479 11.50700 11.49847 11.49098 11.48624 11.48397 11.48389
## [273] 11.48572 11.48919 11.49402 11.49993 11.50665 11.51388 11.52137 11.52883
## [281] 11.53598 11.54255 11.54825 11.55282 11.55597 11.55742 11.55690 11.55414
## [289] 11.54884 11.54199 11.53474 11.52715 11.51925 11.51108 11.50267 11.49406
## [297] 11.48529 11.47640 11.46743 11.45841 11.44938 11.44039 11.43145 11.42263
## [305] 11.41394 11.40543 11.39715 11.38912 11.37942 11.36638 11.35034 11.33168
## [313] 11.31077 11.28797 11.26366 11.23819 11.21194 11.18527 11.15855 11.13214
## [321] 11.10643 11.08176 11.05851 11.03705 11.01775 11.00096 10.98707 10.97643
## [329] 10.96941 10.96416 10.95864 10.95300 10.94735 10.94184 10.93660 10.93176
## [337] 10.92745 10.92380 10.92094 10.91901 10.91814 10.91846 10.92010 10.92320
## [345] 10.92788 10.93460 10.94360 10.95471 10.96775 10.98253 10.99889 11.01665
## [353] 11.03562 11.05563 11.07651 11.09807 11.12013 11.14253 11.16508 11.18760
## [361] 11.20992 11.23186 11.25324 11.27388 11.29362 11.31225 11.33163 11.35350
## [369] 11.37764 11.40377 11.43166 11.46104 11.49167 11.52330 11.55567 11.58854
## [377] 11.62164 11.65473 11.68756 11.71988 11.75143 11.78195 11.81121 11.83895
## [385] 11.86491 11.88885 11.91051 11.93165 11.95410 11.97769 12.00222 12.02754
## [393] 12.05345 12.07980 12.10640 12.13307 12.15964 12.18594 12.21180 12.23702
## [401] 12.26145 12.28490 12.30719 12.32816 12.34763 12.36542 12.38136 12.39526
## [409] 12.40816 12.42114 12.43413 12.44705 12.45982 12.47239 12.48466 12.49658
## [417] 12.50806 12.51903 12.52941 12.53915 12.54815 12.55635 12.56367 12.57004
## [425] 12.57539 12.57964 12.58272 12.58406 12.58326 12.58051 12.57597 12.56984
## [433] 12.56229 12.55349 12.54364 12.53291 12.52148 12.50953 12.49723 12.48478
## [441] 12.47235 12.46011 12.44826 12.43696 12.42640 12.41676 12.40822 12.40095
## [449] 12.39317 12.38315 12.37119 12.35759 12.34265 12.32667 12.30995 12.29278
## [457] 12.27547 12.25831 12.24160 12.22564 12.21073 12.19717 12.18526 12.17529
## [465] 12.16573 12.15496 12.14312 12.13035 12.11681 12.10262 12.08795 12.07294
## [473] 12.05772 12.04245 12.02728 12.01234 11.99778 11.98375 11.97039 11.95785
## [481] 11.94628 11.93581 11.92659 11.91878 11.91251 11.90675 11.90047 11.89377
## [489] 11.88678 11.87961 11.87237 11.86519 11.85817 11.85145 11.84512 11.83932
## [497] 11.83415 11.82973 11.82619 11.82363 11.82217 11.82193 11.82303 11.82558
## [505] 11.82983 11.83586 11.84353 11.85269 11.86320 11.87493 11.88772 11.90144
## [513] 11.91594 11.93109 11.94673 11.96273 11.97895 11.99525 12.01148 12.02749
## [521] 12.04316 12.05833 12.07287 12.08662 12.09946 12.11124 12.12181 12.13306
## [529] 12.14679 12.16277 12.18079 12.20060 12.22198 12.24470 12.26853 12.29325
## [537] 12.31861 12.34440 12.37038 12.39633 12.42201 12.44720 12.47167 12.49518
## [545] 12.51752 12.53844 12.55772 12.57514 12.59046 12.60345 12.61388 12.62153
## [553] 12.62617 12.62883 12.63075 12.63196 12.63247 12.63232 12.63154 12.63016
## [561] 12.62820 12.62569 12.62267 12.61915 12.61518 12.61077 12.60596 12.60078
## [569] 12.59524 12.58939 12.58325 12.57685 12.57022 12.56338 12.55447 12.54191
## [577] 12.52617 12.50771 12.48701 12.46452 12.44073 12.41609 12.39108 12.36617
## [585] 12.34181 12.31849 12.29666 12.27680 12.25937 12.24485 12.23006 12.21180
## [593] 12.19046 12.16644 12.14014 12.11195 12.08228 12.05152 12.02007 11.98832
## [601] 11.95667 11.92553 11.89529 11.86634 11.83909 11.81393 11.79126 11.77147
## [609] 11.75497 11.74216 11.73148 11.72117 11.71123 11.70170 11.69259 11.68394
## [617] 11.67575 11.66807 11.66091 11.65429 11.64824 11.64278 11.63793 11.63373
## [625] 11.63018 11.62732 11.62517 11.62375 11.62309 11.62320 11.62441 11.62692
## [633] 11.63065 11.63549 11.64134 11.64809 11.65566 11.66394 11.67282 11.68221
## [641] 11.69201 11.70212 11.71243 11.72284 11.73326 11.74358 11.75370 11.76352
## [649] 11.77295 11.78188 11.79020 11.79848 11.80732 11.81668 11.82654 11.83685
## [657] 11.84759 11.85873 11.87023 11.88206 11.89420 11.90659 11.91923 11.93206
## [665] 11.94507 11.95821 11.97146 11.98495 11.99881 12.01304 12.02764 12.04259
## [673] 12.05790 12.07355 12.08955 12.10588 12.12253 12.13951 12.15680 12.17440
## [681] 12.19231 12.21051 12.22900 12.24778 12.26683 12.28616 12.30576 12.32561
## [689] 12.34572 12.36607 12.38667 12.40750 12.42857 12.44983 12.47127 12.49291
## [697] 12.51475 12.53681 12.55909 12.58161 12.60437 12.62739 12.65068 12.67424
## [705] 12.69810 12.72225 12.74671 12.77149 12.79660
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")